﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace ArmyCreator.ViewModel
{
    public class ModelObject : INotifyPropertyChanged
    {
        private Dictionary<string, object> properties = new Dictionary<string, object>();

        protected T GetValue<T>(string propertyName, T defaultValue)
        {
            T value = defaultValue;

            if (properties.ContainsKey(propertyName))
            {
                value = (T)properties[propertyName];
            }
            else
            {
                // initialise dans le store dans le cas d'une collection
                if (defaultValue != null && defaultValue is IEnumerable)
                {
                    properties[propertyName] = value;
                }
            }

            return value;
        }

        protected void SetValue(string propertyName, object value)
        {
            if (properties[propertyName] != value)
            {
                properties[propertyName] = value;
                Notify(propertyName);
            }
        }

        #region Notification
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
        #endregion
    }
}
